package cn.com.demo.poi;

import org.apache.poi.ss.usermodel.*;

import java.io.FileInputStream;
import java.io.IOException;

public class FindValInCell {
    private static String filePath = "E:\\pactera\\doc\\项目\\平顶山银行\\其他资料\\备份-20201230\\客户主题代码差异分析资料\\核心系统代码数据.xlsx";
    private static String findVal = "1";
    public static void main(String[] args) throws IOException {
        // Create a FileInputStream to read the Excel file
        FileInputStream file = new FileInputStream(filePath);
        // Create a Workbook object to access the Excel file
        Workbook workbook = WorkbookFactory.create(file);
        int findCount = 0;
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
            Sheet sheet = workbook.getSheetAt(i);
            for (Row row:sheet) {
                for (Cell cell : row) {
                    if (cell.getStringCellValue() != null && cell.getStringCellValue().contains(findVal)) {
                        System.out.println("数据在Sheet页【" + sheet.getSheetName() + "】的"+(row.getRowNum()+1)+"行"+(cell.getColumnIndex()+1)+"列，原值：" + cell.getStringCellValue());
                        workbook.close();
                        findCount++;
                    }
                }
            }
        }
        file.close();
        System.out.println(String.format("搜索完毕，共找到%d条,耗时%d毫秒",findCount,System.currentTimeMillis() - startTime));
    }
}
